package com.yyds.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyds.common.MyJSON;
import com.yyds.mapper.PersonResidentMapper;
import com.yyds.mapper.VaccineStatusMapper;
import com.yyds.pojo.PersonResident;
import com.yyds.service.PersonResidentService;
import com.yyds.vo.EchartPieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 花君瑜
 * @date 2022/10/14 08:46:24
 * @desc
 **/
@Service("personResidentService")
public class PersonResidentServiceImpl implements PersonResidentService {
    @Autowired
    private PersonResidentMapper personResidentMapper;
    @Autowired
    private VaccineStatusMapper vaccineStatusMapper;

    @Override
    public MyJSON<PersonResident> queryAllPersonResidentService(Map<String, Object> params) {
        //mybatis分页 插件的使用
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);
        List<PersonResident> personResidents = personResidentMapper.queryAllPersonResidentMapper(params);
        System.out.println("List<PersonResident> personResidents =" + personResidents);
        //mybatis自带的分页数据结构
        PageInfo<PersonResident> personResidentPageInfo = new PageInfo<>(personResidents);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        MyJSON<PersonResident> personResidentMyJSON = new MyJSON<>();
        personResidentMyJSON.setCode(0);
        personResidentMyJSON.setMsg("居民信息分页列表数据");
        personResidentMyJSON.setCount(personResidentPageInfo.getTotal());
        personResidentMyJSON.setData(personResidentPageInfo.getList());
        System.out.println(personResidentMyJSON);
        return personResidentMyJSON;
    }

    @Override
    public Integer deletePersonResidentService(Integer resident_id) {

        return personResidentMapper.deletePersonResidentMapper(resident_id);
    }

    @Override
    public Integer addPersonResidentService(PersonResident personResident, String status_status) {
        System.out.println("public Integer addPersonResidentService(PersonResident personResident, String status_status) = " + status_status);
        if (status_status != null && !status_status.equals("")) {

            Integer result = personResidentMapper.addPersonResidentMapper(personResident);
            if (result != 0) {
                Integer resident_id = personResidentMapper.selectPersonResidentId(personResident.getResident_name());
                System.out.println("personResidentMapper.selectPersonResidentId(personResident.getResident_name()) = "+resident_id);
                Integer integer = personResidentMapper.addWithStatusPersonResidentMapper(resident_id, status_status);
                return integer;
            }
        }
        return personResidentMapper.addPersonResidentMapper(personResident);
    }

    @Override
    public Integer updatePersonResidentService(PersonResident personResident) {
        return personResidentMapper.updatePersonResidentService(personResident);
    }

    @Override
    public Integer deleteAllPersonResidentService(String str) {
        //1-2-3
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
        }
        return personResidentMapper.deleteAllPersonResidentMapper(list);
    }

    @Override
    public List<EchartPieVo> queryPersonResidentService() {
        return personResidentMapper.queryPersonResidentMapper();
    }


}
